package com.controller;

import com.pojo.Bill;
import com.service.BillService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR: 我是LGXHXE
 * @Data:
 * @Version:
 */
@WebServlet("/bill/*")
public class BillServlet extends BaseServlet {
    private BillService service = new BillService();
    private int countcurrentPageNo = 0;
    private List<Bill> list = service.getbills();
    private Integer pageSize = 8;

    public void billlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 调用业务层查询所有订单
        countcurrentPageNo = 0;
        method2(req);
        //3. 转发
        req.getRequestDispatcher("/jsp/billlist.jsp").forward(req, resp);
    }

    public void billlistUp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        countcurrentPageNo--;
        if (countcurrentPageNo >= 0) {
            //2. 存域中
            method2(req);
            //3. 转发
            req.getRequestDispatcher("/jsp/billlist.jsp").forward(req, resp);
        } else {
            countcurrentPageNo++;
            resp.sendRedirect(req.getContextPath() + "/bill/billlistLast");
        }
    }

    public void billlistDown(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        countcurrentPageNo++;
        if (countcurrentPageNo < (list.size() - 1) / pageSize + 1) {
            //2. 存域中
            method2(req);
            //3. 转发
            req.getRequestDispatcher("/jsp/billlist.jsp").forward(req, resp);
        } else {
            countcurrentPageNo--;
            resp.sendRedirect(req.getContextPath() + "/bill/billlist");
        }
    }

    public void billlistLast(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 调用业务层查询所有订单
        countcurrentPageNo = (list.size() - 1) / pageSize;
        //2. 存域中
        method2(req);
        //3. 转发
        req.getRequestDispatcher("/jsp/billlist.jsp").forward(req, resp);
    }

    public void billlistGo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 调用业务层查询所有订单
        String currentPageNo1 = req.getParameter("inputPage");
        if (currentPageNo1.equals("")) {
            resp.sendRedirect(req.getContextPath() + "/bill/billlist");
        } else {
            int currentPageNo = Integer.parseInt(currentPageNo1);
            if (currentPageNo >= 0 && currentPageNo <= (list.size() - 1) / pageSize) {
                countcurrentPageNo = currentPageNo;
                //2. 存域中
                method2(req);
                //3. 转发
                req.getRequestDispatcher("/jsp/billlist.jsp").forward(req, resp);
            } else {
                resp.sendRedirect(req.getContextPath() + "/bill/billlist");
            }
        }
    }

    public void method2(HttpServletRequest req) throws ServletException, IOException {
        ArrayList<Bill> bills = new ArrayList<>();
        for (int i = 0; i < pageSize; i++) {
            if (countcurrentPageNo * pageSize + i < list.size()) {
                bills.add(list.get(countcurrentPageNo * pageSize + i));
            } else {
                break;
            }
        }
        req.getSession().setAttribute("BILL_LIST", bills);
        req.getSession().setAttribute("totalCount", list.size());
        req.getSession().setAttribute("currentPageNo", countcurrentPageNo);
    }

    public void billSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer billCode = Integer.parseInt(req.getParameter("billCode"));
        String productName = req.getParameter("productName");
        String unit = req.getParameter("unit");
        Integer number = Integer.parseInt(req.getParameter("number"));
        BigDecimal totalPrice = new BigDecimal(req.getParameter("totalPrice"));
        String providerName = req.getParameter("providerName");
        Integer isPayment = Integer.parseInt(req.getParameter("isPayment"));
        Date date = new Date(System.currentTimeMillis());
        Bill bill = new Bill(null, billCode, productName, unit, number, totalPrice, providerName, isPayment, date, null);
        service.save(bill);
        list = service.getbills();
        resp.sendRedirect(req.getContextPath() + "/bill/billlist");
    }

    public void billCheck(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 调用业务层查询所有订单
        Bill bill = service.getbill(req.getParameter("id"));
        //2. 存域中
        req.getSession().setAttribute("BILL", bill);
        //3. 转发
        req.getRequestDispatcher("/jsp/billview.jsp").forward(req, resp);
    }

    public void billCheckForUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 调用业务层查询所有订单
        Bill bill = service.getbill(req.getParameter("id"));
        //2. 存域中
        req.getSession().setAttribute("BILLFU", bill);
        //3. 转发
        req.getRequestDispatcher("/jsp/billmodify.jsp").forward(req, resp);
    }

    public void billUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        Integer billCode = Integer.parseInt(req.getParameter("billCode"));
        String productName = req.getParameter("productName");
        String unit = req.getParameter("unit");
        Integer number = Integer.parseInt(req.getParameter("number"));
        BigDecimal totalPrice = new BigDecimal(req.getParameter("totalPrice"));
        String providerName = req.getParameter("providerName");
        Integer isPayment = Integer.parseInt(req.getParameter("isPayment"));
        Bill bill = new Bill(id, billCode, productName, unit, number, totalPrice, providerName, isPayment, service.getbill(req.getParameter("id")).getCreationDate(), null);
        service.update(bill);
        list = service.getbills();
        resp.sendRedirect(req.getContextPath() + "/bill/billlist");
    }

    public void billDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        service.removeEmp(req.getParameter("id"));
        list = service.getbills();
        resp.sendRedirect(req.getContextPath() + "/bill/billlist");
    }

    public void billSearch(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        String qpn = req.getParameter("queryProductName");
        String qpi = req.getParameter("queryProviderId");
        String qip = req.getParameter("queryIsPayment");
        boolean n = qpn.equals("");
        boolean i = qpi.equals("");
        boolean p = qip.equals("0");
        String sql = "";
        List<Bill> list1 = null;
        req.getSession().setAttribute("queryProductName", qpn);
        req.getSession().setAttribute("queryProviderId", qpi);
        req.getSession().setAttribute("queryIsPayment", qip);
        if (n) {
            if (i) {
                if (!p) {
                    sql = "select * from bill where isPayment=?";
                    list1 = service.search(sql, qip);
                } else {
                    list1 = service.getbills();
                }
            } else {
                if (!p) {
                    sql = "select * from bill where isPayment=? and pid=?";
                    list1 = service.search(sql, qip, qpi);
                } else {
                    sql = "select * from bill where pid=?";
                    list1 = service.search(sql, qpi);
                }
            }
        } else {
            qpn = "%" + qpn + "%";
            if (i) {
                if (p) {
                    sql = "select * from bill where productName like ?";
                    list1 = service.search(sql, qpn);
                } else {
                    sql = "select * from bill where productName like ? and isPayment=?";
                    list1 = service.search(sql, qpn, qip);
                }
            } else {
                if (p) {
                    sql = "select * from bill where productName like ? and pid?";
                    list1 = service.search(sql, qpn, qpi);
                } else {
                    sql = "select * from bill where productName like ? and isPayment=? and pid=?";
                    list1 = service.search(sql, qpn, qip, qip);
                }
            }
        }
        //2. 存域中
        list = list1;
        req.getSession().setAttribute("BILL_LIST", list);
        //3. 转发
        resp.sendRedirect(req.getContextPath() + "/bill/billlist");
    }
}
